import http from './interface'
import store from '@/store'
//设置请求前拦截器
http.interceptor.request = (config) => {
	console.log("====--++++++--"+store.state.userInfo.token)
	config.header = {
		'token': store.state.userInfo.token,
	}

}
//设置请求结束后拦截器
http.interceptor.response = (response) => {
	console.log("=============--------"+store.state.userInfo.token)
	uni.hideLoading()
	if(response.statusCode === 200){
		if ( response.data.code === 20001 || response.data.code === 20002) {
			//未登录
			uni.showToast({
				title: response.data.message,
				duration: 1200,
				icon: 'none'
			})
			setTimeout(function() {
				uni.navigateTo({
					url: '/pages/login/login',
					animationType: 'slide-in-bottom',
					"animationDuration": 450
				});
			}, 450);
			store.commit('logout')
			return
		}
	}else{
		uni.showToast({
			title: '网络好像不太通畅~',
			duration: 1200,
			icon: 'none'
		})
	}

}



/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 *
 */

// 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
export const test = (data) => {
	/* http.config.baseUrl = "http://localhost:8080/api/"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		config.header = {
			"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
		}
	} */
	//设置请求结束后拦截器
	// http.interceptor.response = (response) => {
	// 	console.log('个性化response....')
	// 	//判断返回状态 执行相应操作
	// 	return response;
	// }
	return http.request({
		baseUrl: 'https://unidemo.dcloud.net.cn/',
		url: 'ajax/echo/text?name=uni-app',
		dataType: 'text',
		data,
	})
}

export const getfrontPage = (data, pageNum, pageSize) => {
	// http.interceptor.request = (config) => {
	// 	config.header = {
	// 		'pageNum': pageNum,
	// 		'pageSize': pageSize,
	// 		'token':store.state.userInfo.token
	// 	}
	// }
	
	return http.request({
		url: 'api/Applets/get_frontPage',
		method: 'GET',
		data
	})
}
export const getUserInfo = (data) => {
	
	return http.request({
		url: 'api/mall/user/get_user_Info',
		method: 'POST',
		data,
	})
}

export const openusermember = (data) => {

	return http.request({
		url: 'api/mall/user_member/open_user_member',
		method: 'POST',
		data,
	})
}
export const getuserrelationList = (data) => {

	return http.request({
		url: 'api/mall/user_relation/get_user_relation',
		method: 'POST',
		data,
	})
}
export const getrelation = (data) => {

	return http.request({
		url: 'api/mall/user_relation/get_relation',
		method: 'POST',
		data,
	})
}
export const adduserrelation = (data) => {

	return http.request({
		url: 'api/mall/user_relation/add_user_relation',
		method: 'POST',
		data,
	})
}
export const walletrecorddetail = (data) => {

	return http.request({
		url: 'api/mall/bill/get_user_wallet_record',
		method: 'POST',
		data,
	})
}
export const rechargerecord = (data) => {

	return http.request({
		url: 'api/mall/user_member/get_recharge_record',
		method: 'POST',
		data,
	})
}
export const getQRcode = (data) => {

	return http.request({
		url: 'api/mall/member_recommend/get_QR_code',
		method: 'POST',
		data,
	})
}
export const getrecommendedrecord = (data) => {

	return http.request({
		url: 'api/mall/member_recommend/get_recommended_record',
		method: 'POST',
		data,
	})
}
export const deleteuserrelation = (data) => {

	return http.request({
		url: 'api/mall/user_relation/delete_user_relation',
		method: 'POST',
		data,
	})
}
export const addusermoney = (data) => {

	return http.request({
		url: 'api/mall/user_member/add_user_money',
		method: 'POST',
		data,
	})
}
export const updateUserInfo = (data) => {

	return http.request({
		url: 'api/mall/user/update_user',
		method: 'POST',
		data,
	})
}
export const bindrecommenduser = (data) => {
	
	return http.request({
		url: 'api/mall/member_recommend/bind_recommend_user',
		method: 'POST',
		data,
	})
}
export default {
	getUserInfo,
	openusermember,
	getuserrelationList,
	getrelation,
	adduserrelation,
	walletrecorddetail,
	rechargerecord,
	getQRcode,
	getrecommendedrecord,
	deleteuserrelation,
	addusermoney,
	updateUserInfo,
	bindrecommenduser
}
